import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {Logger} from "../../frame/apiModel/config/Logger";
import {SpecialEffectType} from "../../type/Type";
import {UnitType} from "../../frame/dataSource/DataModel";

export default class DSan extends AbilityModel {
    // abilityId: string = Configer.getFinal(FinalKey.bigJump) as string;


    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success = false;

        const costMana = 100;
        const maxRange = 300;

        const distanceUnit = 100;
        const toSpeed = 0.01;

        if (activeUnit.mana < costMana) {
            Logger.toastProduct( "魔法值不足", activePlayer);
            return;
        }
        if (activeUnit.isVertigo()) {
            Logger.toastProduct( "眩晕状态无法D闪", activePlayer);
            return;
        }
        //开始D 闪
        // SetUnitState(hero, UNIT_STATE_MANA, (GetUnitState(hero, UNIT_STATE_MANA) - cfgData.costMana));
        const loc = activeUnit.loc;
        let xp0: number = loc.x;
        let yp0: number = loc.y;

        let xp1: number = spellLoc.x;
        let yp1: number = spellLoc.y;

        let dx: number = xp1 - xp0;
        let dy: number = yp1 - yp0;
        let distance: number = MathUtil.squareRoot(dx * dx + dy * dy)

        if (distance > maxRange) {
            distance = maxRange
        }
        let j = MathUtil.atan2(yp1 - yp0, xp1 - xp0)
        // let tx: effect = AddSpecialEffect("cf" + Math.min(id + 1, 4) + ".mdx", xp0, yp0);
        // EXSetEffectSize(tx, 1.25);
        // EXSetUnitMoveType(hero, 0x00);
        // EXEffectMatRotateZ(tx, j)
        let s: number = 0;
        let xp2: number;
        let yp2: number;

        const callBack = function () {
            const newLoc = activeUnit.loc;
            xp0 = newLoc.x;
            yp0 = newLoc.y;
            xp2 = xp0 + distanceUnit * MathUtil.cos(j);
            yp2 = yp0 + distanceUnit * MathUtil.sin(j);
            // let x1 = xp0 + 100 * MathUtil.cos(j);
            // let y1 = yp0 + 100 * MathUtil.sin(j);

            if (activeUnit?.isAliveInBattle()) {
                s = s + distanceUnit;
                activeUnit.loc = new LocModel(xp2, yp2);
                activeUnit.facing = j;
                // EXSetEffectXY(tx, xp2, yp2)
            }
        }
        callBack();
        // const timerOpt = new TimeControllerOpt(undefined, callBack, 1);
        // TimeController.getInstance().startTimer({time: toSpeed, opt: timerOpt});
        return success;
    }
}